package com.example.springbootweb_comprehensiveexample.controller;

import com.example.springbootweb_comprehensiveexample.anno.Log;
import com.example.springbootweb_comprehensiveexample.pojo.Emp;
import com.example.springbootweb_comprehensiveexample.pojo.PageBean;
import com.example.springbootweb_comprehensiveexample.pojo.Result;
import com.example.springbootweb_comprehensiveexample.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController

public class EmpController {

    @Autowired
    private EmpService empService;
    
    //查询员工（分页 + 条件）
    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                   @RequestParam(defaultValue = "10") Integer pageSize,
                   String name,
                   Short gender,
                   //@DateTimeFormat用来指定日期的传递格式
                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){

        log.info("分页查询，参数: {},{},{},{}", page, pageSize, name, gender, begin, end);
        
        //调用service
       PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
       return Result.success(pageBean);
    } 

    //删除员工
    @Log
    @DeleteMapping("/emps/{ids}")//路径参数要加注解@PathVariable
    public Result delete(@PathVariable List<Integer> ids){  //ids是一个数组集合
        log.info("删除员工：{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    //添加员工
    @Log
    @PostMapping("/emps")
    public Result add(@RequestBody Emp emp){
        log.info("新增员工,emp{}", emp);
        empService.add(emp);
        return Result.success();
    }

    //根据id查询员工
    @GetMapping("/emps/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("根据id查询员工：{}", id);
        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }

    //根据id更新员工
    @Log
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp){
        log.info("更新员工信息，{}", emp);
        empService.update(emp);
        return Result.success();
    }

}
